﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Twitter_Data_Crawler
{
    public static class clsCommon
    {
        public static int fncLevenshteinDistance1(string s, string t)
        {
            int n = s.Length;
            int m = t.Length;
            int[,] d = new int[n + 1, m + 1];

            // Step 1
            if (n == 0)
            {
                return m;
            }

            if (m == 0)
            {
                return n;
            }

            // Step 2
            for (int i = 0; i <= n; d[i, 0] = i++)
            {
            }

            for (int j = 0; j <= m; d[0, j] = j++)
            {
            }

            // Step 3
            for (int i = 1; i <= n; i++)
            {
                //Step 4
                for (int j = 1; j <= m; j++)
                {
                    // Step 5
                    int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                    // Step 6
                    d[i, j] = Math.Min(
                        Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                        d[i - 1, j - 1] + cost);
                }
            }
            // Step 7
            return d[n, m];
        }

        public static int fncLevenshteinDistance2(String a, String b)
        {

            if (string.IsNullOrEmpty(a))
            {
                if (!string.IsNullOrEmpty(b))
                {
                    return b.Length;
                }
                return 0;
            }

            if (string.IsNullOrEmpty(b))
            {
                if (!string.IsNullOrEmpty(a))
                {
                    return a.Length;
                }
                return 0;
            }

            int cost;
            int[,] d = new int[a.Length + 1, b.Length + 1];
            int min1;
            int min2;
            int min3;

            for (int i = 0; i <= d.GetUpperBound(0); i += 1)
            {
                d[i, 0] = i;
            }

            for (int i = 0; i <= d.GetUpperBound(1); i += 1)
            {
                d[0, i] = i;
            }

            for (int i = 1; i <= d.GetUpperBound(0); i += 1)
            {
                for (int j = 1; j <= d.GetUpperBound(1); j += 1)
                {
                    cost = Convert.ToInt32(!(a[i - 1] == b[j - 1]));

                    min1 = d[i - 1, j] + 1;
                    min2 = d[i, j - 1] + 1;
                    min3 = d[i - 1, j - 1] + cost;
                    d[i, j] = Math.Min(Math.Min(min1, min2), min3);
                }
            }

            return d[d.GetUpperBound(0), d.GetUpperBound(1)];

        }

        //public static string fncCnvToDbString(string strValue)
        //{
        //    string strResult = "";
        //    try
        //    {
        //        if (string.IsNullOrEmpty(strValue)) return "";
        //        return strResult = strValue.Replace("'", "''");
        //    }
        //    catch { return ""; }
        //}

        /// <summary>
        /// Show Error Message
        /// </summary>
        /// <param name="strModule">Method/function name</param>
        /// <param name="strMsg">Custom message to show</param>
        /// <returns></returns>
        public static bool fncMsgError(string strClass, string strModule, string strMsg)
        {
            bool blnResult = false;

            try
            {
                string strMsgShow = string.Format("Class : {0} \nMethod : {1} \nMessage : \n{2}", strClass, strModule, strMsg);

                //MessageBox.Show(strMsgShow, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(strMsgShow, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                
                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show ERROR message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgError(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show information message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgInfo(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show warning message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgWarning(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show confirm message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgConfirm(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //if (MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) return true;
                if (MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) return true;

                return false;
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show open file dialog
        /// </summary>
        /// <param name="strFilter"></param>
        /// <returns></returns>
        public static string fncOpenFile(string strFilter)
        {
            string strResult = "";
            try
            {
                using (OpenFileDialog objOpen = new OpenFileDialog())
                {
                    objOpen.Filter = strFilter;
                    if (objOpen.ShowDialog() == DialogResult.OK) strResult = objOpen.FileName;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            return strResult;
        }

        /// <summary>
        /// Kiem tra chuoi la null
        /// Author: QuyetLT
        /// </summary>
        /// <param name="strText">Chuoi dau vao</param>
        /// <param name="strMsg">Message thong bao</param>
        /// <returns>Bool</returns>
        public static bool fncIsBlank(string strText, string strMsg = null)
        {
            bool blnResult = false;
            try
            {
                if (DBNull.Value.Equals(strText)) return true;

                if (string.IsNullOrEmpty(strText))
                {
                    //show message and set focus if available
                    if (strMsg != null) fncMsgWarning(strMsg);
                    blnResult = true;
                }
            }
            catch (Exception ex) { throw ex; }
            return blnResult;
        }

        /// <summary>
        /// Convert gia tri sang DbDatetime
        /// </summary>
        /// <param name="dtValue"></param>
        /// <returns></returns>
        public static object fncCnvToDbDatetime(DateTime dtValue)
        {
            if (dtValue <= DateTime.MinValue || dtValue >= DateTime.MaxValue) return "NULL";//System.Data.SqlTypes.SqlDateTime.Null;

            return dtValue.ToString("yyyy/MM/dd hh:mm:ss");
            //return dtValue;
        }

        /// <summary>
        /// Convert gia tri sang DbString
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static object fncCnvToDbString(string strInput)
        {
            if (DBNull.Value.Equals(strInput)) return "";
            if (string.IsNullOrEmpty(strInput)) return "";

            strInput = strInput.Replace("[", "[[]");
            //strInput = "'" + strInput.Replace("'", "''") + "'";
            strInput = strInput.Replace("'", "''");

            return strInput;
        }

        /// <summary>
        /// Convert gia tri sang Dbbit
        /// Author: QuyetLT
        /// </summary>
        /// <param name="blnInput">Gia tri vao</param>
        /// <returns>1 hoac 0</returns>
        public static object fncCnvToDbBit(bool blnInput)
        {
            if (blnInput) return 1;
            return 0;
        }

        /// <summary>
        /// Convert gia tri sang DbImage
        /// </summary>
        /// <param name="objImage"></param>
        /// <returns></returns>
        public static object fncCnvToDbImage(Image objImage)
        {
            if (objImage == null) return "";
            return objImage;
        }

        /// <summary>
        /// Chuyen doi sang string
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>string</returns>
        public static string fncCnvNullToString(object objInput)
        {
            string strResult = "";

            try
            {
                //tra ve "" neu input la null
                if (Convert.IsDBNull(objInput)) return "";
                if (objInput == null) return "";
                strResult = Convert.ToString(objInput);
            }
            catch (Exception ex) { return ""; }

            return strResult;
        }

        /// <summary>
        /// Chuyen doi sang byte[]
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>byte[]</returns>
        public static byte[] fncCnvNullToByteArray(object objInput)
        {
            byte[] strResult = null;

            try
            {
                //tra ve "" neu input la null
                if (Convert.IsDBNull(objInput)) return strResult;
                if (objInput == null) return strResult;
                strResult = (byte[])(objInput);
            }
            catch (Exception ex) { return strResult; }

            return strResult;
        }

        /// <summary>
        /// Chuyen doi sang int
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>int</returns>
        public static int fncCnvNullToInt(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            int intResult = 0;
            int.TryParse(strTemp, out intResult);
            return intResult;
        }

        /// <summary>
        /// Chuyen doi sang double
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>double</returns>
        public static double fncCnvNullToDbl(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            double dblResult = 0;
            double.TryParse(strTemp, out dblResult);
            return dblResult;
        }

        /// <summary>
        /// Chuyen doi sang float
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>float</returns>
        public static float fncCnvNullToFloat(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            float fltResult = 0F;
            float.TryParse(strTemp, out fltResult);
            return fltResult;
        }

        /// <summary>
        /// Chuyen doi sang bool
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>bool</returns>
        public static bool fncCnvNullToBool(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput).Trim();

            if (strTemp == "1" || strTemp.ToLower() == "true") return true;
            if (strTemp == "0" || strTemp.ToLower() == "false") return false;

            bool blnResult = false;
            bool.TryParse(strTemp, out blnResult);
            return blnResult;
        }

        /// <summary>
        /// Chuyen doi bool sang int
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>bool</returns>
        public static int fncCnvBoolToInt(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput).Trim().ToLower();

            if (strTemp == "true") return 1;
            if (strTemp == "false") return 0;

            return 0;
        }

        /// <summary>
        /// Chuyen doi sang Datetime
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>DateTime</returns>
        public static DateTime fncCnvNullToDatetime(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            DateTime dtResult = DateTime.MinValue;
            DateTime.TryParse(strTemp, out dtResult);
            return dtResult;
        }

        /// <summary>
        /// In hoa chữ cái đầu tiên
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string fncCapitalizeFirst(string strValue)
        {
            System.Globalization.TextInfo ti = new System.Globalization.CultureInfo("en-US", false).TextInfo;
            return ti.ToTitleCase(strValue);
        }

    }
}
